<template>
  <div>
    <div class="app-container" style="height: 100%">
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="农作物名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入农作物名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="tableData">
        <el-table-column label="序号" type="index" width="80" />
        <el-table-column
          prop="name"
          align="center"
          label="农作物名称"
        ></el-table-column>
        <el-table-column
          prop="kc"
          align="center"
          label="作物系数"
        ></el-table-column>
        <el-table-column
          prop="profit"
          align="center"
          label="单位水量收益系数"
        />
        <el-table-column
          prop="rn"
          align="center"
          label="作物表面的太阳净辐射"
        />
        <el-table-column prop="createAt" align="center" label="创建时间" />
        <el-table-column prop="updateAt" align="center" label="更新时间" />
        <el-table-column
          label="操作"
          align="center"
          width="150px"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >

            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        :title="title"
        v-model="open"
        width="400px"
        append-to-body
        destroy-on-close
      >
        <el-form ref="formRef" :model="form" label-width="160px">
          <el-form-item label="农作物名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入农作物名称" />
          </el-form-item>

          <el-form-item label="作物系数" prop="kc">
            <el-input-number v-model="form.kc" placeholder="请输入作物系数" />
          </el-form-item>
          <el-form-item label="单位水量收益系数" prop="profit">
            <el-input-number
              v-model="form.profit"
              placeholder="请输入单位水量收益系数"
            />
          </el-form-item>
          <el-form-item label="作物表面的太阳净辐射" prop="rn">
            <el-input-number
              v-model="form.rn"
              placeholder="请输入作物表面的太阳净辐射"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>

      <!-- 分页区域 -->
      <el-pagination
        style="float: right"
        @current-change="getList"
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        layout="total, prev, pager, next"
        :total="pagination.total"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import { getNzwList, delNzw, getNzwDetailById, saveNzw } from "@/api/modal/nzw";
import { ElMessage, ElMessageBox } from "element-plus";
const { proxy } = getCurrentInstance();
const tableData = ref([]);
const open = ref(false);
const loading = ref(true);

const showSearch = ref(false);
const title = ref("");
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0,
});

const data = reactive({
  queryParams: {
    name: "",
  },
  rules: {
    name: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
  },
});
const { queryParams } = toRefs(data);
const form = reactive({});

// 更新重置方法，初始化所有字段
function reset() {
  Object.assign(form, {
    id: null,
    name: "",
    kc: 0,
    profit: 0,
    rn: 0,
  });
  proxy.resetForm("formRef");
}

/** 查询列表 */
function getList() {
  loading.value = true;
  getNzwList({ ...pagination, ...queryParams.value }).then((response) => {
    tableData.value = response.data.list;
    pagination.total = response.data.totalCount;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置搜索条件 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加农作物信息";
}

/** 修改按钮 */
function handleUpdate(row) {
  getNzwDetailById(row.id).then((res) => {
    Object.assign(form, res.data);
    open.value = true;
    title.value = "修改农作物属性";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      saveNzw(form).then(() => {
        proxy.$modal.msgSuccess(form.id ? "修改成功" : "新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 删除按钮 */
function handleDelete(row) {
  ElMessageBox.confirm(
    '是否确认删除名称为"' + row.name + '"的数据项?',
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      delNzw([row.id]).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      });
    })
    .catch(() => {
      ElMessage.info("取消删除");
    });
}

getList();
</script>

<style lang="scss" scoped>
.avatar-uploader .avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.avatar-uploader :deep(.el-upload) {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader :deep(.el-upload:hover) {
  border-color: var(--el-color-primary);
}
:deep(.el-icon.avatar-uploader-icon) {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  text-align: center;
}
</style>
